/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/

// ----------------------------------------------------------------------------
// author: Daniel Lerch <dlerch@gmail.com>, http://opendomo.com
// date: Abril 2007
//
// CHANGES:
//
// ----------------------------------------------------------------------------



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>

#include <upnplog.h>

// {{{ gettime()
char *gettime () 
{
   time_t t;
   t=time(&t);
   return ctime(&t);
}
// }}}

// {{{ upnplog_printf()
void upnplog_printf (int logger_type, const char* format, ...) 
{
   va_list lista;
   int debug = 0;
   FILE *log;
   char time[32];
   char type[64];

#ifdef DEBUG
   debug = 1;
#endif

   switch (logger_type) 
   {
      case UPNPLOG_INFO:
         strcpy(type, "INFO");
         break;

      case UPNPLOG_DEBUG:
         if(!debug) return;
         strcpy(type, "DEBUG");
         break;

      case UPNPLOG_ERROR:
         strcpy(type, "ERR");
         break;

      case UPNPLOG_WARNING:
         strcpy(type, "WARN");
         break;
   }


   strncpy(time, gettime(), sizeof(time));
   time[strlen(time)-1]='\0';

   log = fopen(UPNPLOG_FILE , "a");
   if (log == NULL) 
      return;
   else 
   {
      va_start(lista, format);
      fprintf(log, "%s [%s]: ", time, type);
      vfprintf(log, format, lista);
      va_end(lista);
      fclose(log);
   }
}
// }}}


